package com.muhuahealth.controller;

import com.muhuahealth.entity.PipeReconstructionEntity;
import com.muhuahealth.entity.RoadRepairEntity;
import com.muhuahealth.service.PipeReconstructionService;
import com.muhuahealth.service.RoadRepairService;
import com.muhuahealth.utils.ServerResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/3/6 16:00
 */
@Log4j2
@Controller
@RequestMapping("/admin/dataRecord")
public class DataRecordingController {

    @Autowired
    RoadRepairService roadRepairService;

    @Autowired
    PipeReconstructionService pipeReconstructionService;

    @GetMapping("")
    public String dataRecordingPage(HttpServletRequest request){
        request.getSession().setAttribute("path","daily_data");
        log.info("跳转到道路大中修插入页面");
        return "daily_data";
    }

    // todo 没有实现三个功能在同一个按钮时，怎样区分按钮该是哪个功能
    @PostMapping(value = "/addNewRoadData")
    @ResponseBody
    public ServerResponse addNewRoadData(@RequestBody RoadRepairEntity repairEntity) {

        log.info("开始执行添加操作");
        String returnResult = roadRepairService.addNewRoadData(repairEntity);
        try {

            if (StringUtils.isEmpty(repairEntity.getRoadName())
                    || StringUtils.isEmpty(repairEntity.getRoadLength())
                    || StringUtils.isEmpty(repairEntity.getRoadWidth())
                    || StringUtils.isEmpty(repairEntity.getLiqingArea())
                    || StringUtils.isEmpty(repairEntity.getFootpathArea())
                    || StringUtils.isEmpty(repairEntity.getRoadRemark())) {
                return ServerResponse.createError("添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.createSuccess("添加成功", returnResult);
    }
    //

    @RequestMapping(value = "/addNewPipeData", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addNewPipeData(@RequestBody PipeReconstructionEntity reconsEntity) {

        log.info("开始插入排水管线数据");
        String res = pipeReconstructionService.addNewPipeData(reconsEntity);
        if (StringUtils.isEmpty(reconsEntity.getHutongName())
                || StringUtils.isEmpty(reconsEntity.getMaterialName())
                || StringUtils.isEmpty(reconsEntity.getMaterialSpecification())
                || StringUtils.isEmpty(reconsEntity.getNumbers())
                || StringUtils.isEmpty(reconsEntity.getPipeRemark())
                || StringUtils.isEmpty(reconsEntity.getConstructionTechnology())
                || StringUtils.isEmpty(reconsEntity.getPipeSpecialCase())) {
            return ServerResponse.createError("添加失败");
        }
        return ServerResponse.createSuccess("添加成功",res);
    }
}
